package cn.edu.gzccc.movie.service.impl;

import cn.edu.gzccc.movie.domain.Actor;
import cn.edu.gzccc.movie.repository.ActorRepository;
import cn.edu.gzccc.movie.service.ActorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ActorServiceImpl implements ActorService {

    @Autowired
    private ActorRepository actorRepository;

    /**
     * 添加一个演员
     *
     * @param actor
     */
    @Override
    public void add(Actor actor) {
        Actor actor1 = new Actor();
        actor1.setName(actor.getName());
        actor1.setSex(actor.getSex());
        actor1.setBorn(actor.getBorn());
        actorRepository.save(actor);
    }

    /**
     * 删除一个演员
     *
     * @param id
     */
    @Override
    public void delete(Long id) {
        actorRepository.delete(id);
    }

    /**
     * 更新一个演员
     *
     * @param actor
     */
    @Override
    public void update(Actor actor) {
        Actor actor1 = actorRepository.findOne(actor.getId());
        actor1.setName(actor.getName());
        actor1.setSex(actor.getSex());
        actor1.setBorn(actor.getBorn());
        actorRepository.save(actor);
    }

    /**
     * 查找一个演员
     *
     * @param id
     * @return
     */
    @Override
    public Actor findOne(Long id) {
        Actor actor = actorRepository.findOne(id);
        return actor;
    }

    /**
     * 查找所有演员
     *
     * @return
     */
    @Override
    public List<Actor> findAll() {
        List<Actor> list = actorRepository.findAll();
        return list;
    }

    /**
     * 分页
     *
     * @param page
     * @return
     */
    @Override
    public Page<Actor> findActorNoCriteria(Integer page, Integer size) {
        Pageable pageable = new PageRequest(page, size, Sort.Direction.DESC, "id");
        return actorRepository.findAll(pageable);
    }

}
























 /*@Override
    public Page<Actor> findActorNoCriteria(Integer page) {
        Pageable pageable = new PageRequest(page, 5, Sort.Direction.ASC, "id");
        return actorRepository.findAll(pageable);
    }*/